package com.guli.gulimall.product.service.impl;

import com.guli.gulimall.product.dao.AttrDao;
import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.AttrEntity;
import com.guli.gulimall.product.entity.CategoryEntity;
import com.guli.gulimall.product.vo.AttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.AttrGroupDao;
import com.guli.gulimall.product.entity.AttrGroupEntity;
import com.guli.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @SuppressWarnings("all")
    @Autowired
    private CategoryDao categoryDao;

    @SuppressWarnings("all")
    @Autowired
    private AttrDao attrDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params,Long catelogId) {
        QueryWrapper<AttrGroupEntity> w = new QueryWrapper<>();

        if(!catelogId.equals(0l)){
            w.eq("catelog_id",catelogId);
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                w
        );
        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupVo> withattrList(Long catelogId) {
        //1.通过当前分类编号查询分组信息
        QueryWrapper<AttrGroupEntity> w = new QueryWrapper<>();
        w.eq("catelog_id",catelogId);
        List<AttrGroupEntity> attrGroupList = baseMapper.selectList(w);
        //得到了当前分类下所有分组信息
        //2.遍历所有分组信息先把每个分组对象存放到VO中
        List<AttrGroupVo> collect = attrGroupList.stream().map(item -> {
            AttrGroupVo attrGroupVo = new AttrGroupVo();
            BeanUtils.copyProperties(item, attrGroupVo);
            //3.现在没有值数据为每个分组的规格参数集合
            //需要用分组编号查询规格参数
            QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("attr_type", 1);
            wrapper.eq("attr_group_id", item.getAttrGroupId());
            //查询到的是集合
            List<AttrEntity> attrs = attrDao.selectList(wrapper);
            attrGroupVo.setAttrs(attrs);
            return attrGroupVo;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public AttrGroupEntity getById(Serializable id) {
        AttrGroupEntity attrGroupEntity = baseMapper.selectById(id);
        //现在拿到的是第三级分类
        Long catelogId = attrGroupEntity.getCatelogId();
        List<Long> list = new ArrayList<>();
        getLogPath(list,catelogId);//1,2,3  3,2,1
        Collections.reverse(list);
        Long[] ids = list.toArray(new Long[list.size()]);
        attrGroupEntity.setCatelogPath(ids);
        return attrGroupEntity;
    }

    //查询分类表
    private void getLogPath(List<Long> list,Long catelogId){
          list.add(catelogId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        if (!categoryEntity.getParentCid().equals(0l)){
            getLogPath(list,categoryEntity.getParentCid());

        }
    }
}